From 34d5359b99034d29827310685ba5c724c17ade50 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Mon, 16 Feb 2015 21:07:56 +0000 Subject: [PATCH] Move xol to QXMLStreamWriter, event though I'm not at all sure anyone ever actually uses this format... --- gpsbabel/xol.cc | 303 +++++++++++++++++++++--------------------------- 1 file changed, 135 insertions(+), 168 deletions(-) diff --git a/gpsbabel/xol.cc b/gpsbabel/xol.cc index fb08e94c0..6f162f4d9 100644 --- a/gpsbabel/xol.cc +++ b/gpsbabel/xol.cc @@ -1,22 +1,22 @@ /* - Support for Swiss Map # (.xol) format + Support for Swiss Map # (.xol) format - Copyright (C) 2007 Olaf Klein, o.b.klein@gpsbabel.org + Copyright (C) 2007 Olaf Klein, o.b.klein@gpsbabel.org - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ @@ -24,38 +24,37 @@ #include "xmlgeneric.h" #include "jeeps/gpsmath.h" #include "garmin_tables.h" +#include "src/core/file.h" +#include "src/core/xmlstreamwriter.h" + +#include #include static Waypoint* wpt; static route_head* trk; -static gbfile* fout; -static int space; static bounds all_bounds; static short_handle short_h; -static arglist_t xol_args[] = { - ARG_TERMINATOR -}; +static gpsbabel::File* oqfile; +static gpsbabel::XmlStreamWriter* writer; + +static arglist_t xol_args[] = {ARG_TERMINATOR}; #define MYNAME "xol" -static xg_callback xol_shape, xol_shape_end; -static xg_callback xol_waypt, xol_overlay; +static xg_callback xol_shape, xol_shape_end; +static xg_callback xol_waypt, xol_overlay; #define XOL "/overlays/overlay" -static -xg_tag_mapping xol_map[] = { - { xol_overlay, cb_start, XOL }, - { xol_shape, cb_start, XOL "/shapes/*shape" }, - { xol_shape_end, cb_end, XOL "/shapes/*shape" }, - { xol_waypt, cb_start, XOL "/shapes/shape/*points/point" }, - { NULL, (xg_cb_type)0, NULL } -}; - -static void -xol_overlay(xg_string args, const QXmlStreamAttributes* attrv) -{ +static xg_tag_mapping xol_map[] = { + { xol_overlay, cb_start, XOL }, + { xol_shape, cb_start, XOL "/shapes/*shape" }, + { xol_shape_end, cb_end, XOL "/shapes/*shape" }, + { xol_waypt, cb_start, XOL "/shapes/shape/*points/point" }, + { NULL, (xg_cb_type)0, NULL} }; + +static void xol_overlay(xg_string args, const QXmlStreamAttributes* attrv) { if (attrv->hasAttribute("version")) { if (attrv->value("version") != "1.0") { fatal(MYNAME ": Unsupported version %s.\n", @@ -64,9 +63,7 @@ xol_overlay(xg_string args, const QXmlStreamAttributes* attrv) } } -static void -xol_shape(xg_string args, const QXmlStreamAttributes* attrv) -{ +static void xol_shape(xg_string args, const QXmlStreamAttributes* attrv) { if (attrv->hasAttribute("type")) { if (attrv->value("type") == "waypoint") { wpt = new Waypoint; @@ -94,7 +91,8 @@ xol_shape(xg_string args, const QXmlStreamAttributes* attrv) } if (attrv->hasAttribute("timestamp")) { - wpt->creation_time = xml_parse_time(attrv->value("timestamp").toString().toUtf8().constData()); + wpt->creation_time = xml_parse_time( + attrv->value("timestamp").toString().toUtf8().constData()); } if (attrv->hasAttribute("icon")) { @@ -103,9 +101,7 @@ xol_shape(xg_string args, const QXmlStreamAttributes* attrv) } } -static void -xol_shape_end(xg_string args, const QXmlStreamAttributes*) -{ +static void xol_shape_end(xg_string args, const QXmlStreamAttributes*) { if (wpt) { if (trk) { track_add_wpt(trk, wpt); @@ -121,10 +117,8 @@ xol_shape_end(xg_string args, const QXmlStreamAttributes*) } } -static void -xol_waypt(xg_string args, const QXmlStreamAttributes* attrv) -{ - int x=0, y=0; +static void xol_waypt(xg_string args, const QXmlStreamAttributes* attrv) { + int x = 0, y = 0; if (attrv->hasAttribute("y")) { y = attrv->value("y").toString().toInt(); @@ -134,77 +128,45 @@ xol_waypt(xg_string args, const QXmlStreamAttributes* attrv) x = attrv->value("x").toString().toInt(); } - GPS_Math_Swiss_EN_To_WGS84((double)x, (double)y, &wpt->latitude, &wpt->longitude); + GPS_Math_Swiss_EN_To_WGS84(x, y, &wpt->latitude, &wpt->longitude); } -static void -xol_rd_init(const char* fname) -{ +static void xol_rd_init(const char* fname) { trk = NULL; wpt = NULL; xml_init(fname, xol_map, NULL); } -static void -xol_read(void) -{ - xml_read(); -} +static void xol_read(void) { xml_read(); } -static void -xol_rd_deinit(void) -{ - xml_deinit(); -} +static void xol_rd_deinit(void) { xml_deinit(); } /* writer */ -static void -xol_fatal_outside(const Waypoint* wpt) -{ - gbfprintf(fout, "#####\n"); +static void xol_fatal_outside(const Waypoint* wpt) { fatal(MYNAME ": %s (%s) is outside of convertable area \"%s\"!\n", wpt->shortname.isEmpty() ? "Waypoint" : qPrintable(wpt->shortname), pretty_deg_format(wpt->latitude, wpt->longitude, 'd', NULL, 0), gt_get_mps_grid_longname(grid_swiss, MYNAME)); } - -static void -xol_write_time(const Waypoint* wpt) -{ - QString time_string = wpt->CreationTimeXML(); - if (!time_string.isEmpty()) { - gbfprintf(fout, " timestamp=\"%s\"", qPrintable(time_string)); - } -} - -static void -xol_write_string(const QString& name, const QString& str) -{ - if (!str.isEmpty()) { - QString out = name + "=" + strenquote(str, '"'); - gbfputs(out, fout); - } -} - -static void -xol_waypt_bound_calc(const Waypoint* wpt) -{ +static void xol_waypt_bound_calc(const Waypoint* wpt) { waypt_add_to_bounds(&all_bounds, wpt); } -static void -xol_wr_init(const char* fname) -{ - fout = gbfopen(fname, "w", MYNAME); +static void xol_wr_init(const char* fname) { + oqfile = new gpsbabel::File(fname); + oqfile->open(QIODevice::WriteOnly | QIODevice::Text); + + writer = new gpsbabel::XmlStreamWriter(oqfile); + writer->setAutoFormattingIndent(2); + writer->writeStartDocument(); - space = 1; waypt_init_bounds(&all_bounds); short_h = mkshort_new_handle(); - setshort_length(short_h, 1024); /* ??? */ + setshort_length(short_h, 1024); /* ??? */ setshort_badchars(short_h, "\r\n\t"); setshort_mustupper(short_h, 0); setshort_mustuniq(short_h, 1); @@ -213,16 +175,18 @@ xol_wr_init(const char* fname) setshort_defname(short_h, "Waypoint"); } -static void -xol_wr_deinit(void) -{ +static void xol_wr_deinit(void) { mkshort_del_handle(&short_h); - gbfclose(fout); + writer->writeEndDocument(); + delete writer; + writer = NULL; + + oqfile->close(); + delete oqfile; + oqfile = NULL; } -static void -xol_waypt_disp_cb(const Waypoint* wpt) -{ +static void xol_waypt_disp_cb(const Waypoint* wpt) { double x, y; QString name = wpt->shortname; @@ -232,113 +196,116 @@ xol_waypt_disp_cb(const Waypoint* wpt) name = mkshort(short_h, name); } - if (! GPS_Math_WGS84_To_Swiss_EN(wpt->latitude, wpt->longitude, &x, &y)) { + if (!GPS_Math_WGS84_To_Swiss_EN(wpt->latitude, wpt->longitude, &x, &y)) { xol_fatal_outside(wpt); } + writer->writeStartElement("shape"); + writer->writeAttribute("type", "waypoint"); + writer->writeAttribute("name", name); + writer->writeAttribute("comment", wpt->notes); + writer->writeAttribute("icon", wpt->icon_descr); - gbfprintf(fout, "%*snotes); - xol_write_string("icon", wpt->icon_descr); if (wpt->creation_time.isValid()) { - xol_write_time(wpt); + writer->writeAttribute("timestamp", wpt->CreationTimeXML()); } if (wpt->altitude != unknown_alt) { - gbfprintf(fout, " alt=\"%.f\"", wpt->altitude); + writer->writeAttribute("alt", QString::number(wpt->altitude, 'f', 6)); } - gbfprintf(fout, ">\n"); - - gbfprintf(fout, "%*s\n", space++*2, ""); - gbfprintf(fout, "%*s\n", space*2, "", x, y); - gbfprintf(fout, "%*s\n", --space*2, ""); - gbfprintf(fout, "%*s\n", --space*2, ""); + writer->writeStartElement("points"); + writer->writeStartElement("point"); + writer->writeAttribute("x", QString::number(x)); + writer->writeAttribute("y", QString::number(y)); + writer->writeEndElement(); // point + writer->writeEndElement(); // points + writer->writeEndElement(); // shape } -static void -xol_track_hdr_disp_cb(const route_head* trk) -{ - gbfprintf(fout, "%*srte_name); - gbfprintf(fout, " lineSize=\"3\" lineColor=\"#e60000\" lineStyle=\"solid\">\n"); - gbfprintf(fout, "%*s\n", space++*2, ""); +static void xol_track_hdr_disp_cb(const route_head* trk) { + writer->writeStartElement("shape"); + writer->writeAttribute("type", "polyline"); + writer->writeAttribute("lineSize", "3"); + writer->writeAttribute("lineColor", "#e60000"); + writer->writeAttribute("lineStyle", "solid"); + writer->writeStartElement("waypoints"); } -static void -xol_track_tlr_disp_cb(const route_head* trk) -{ - gbfprintf(fout, "%*s\n", --space*2, ""); - gbfprintf(fout, "%*s\n", --space*2, ""); +static void xol_track_tlr_disp_cb(const route_head* trk) { + writer->writeEndElement(); // waypoints + writer->writeEndElement(); // shape } -static void -xol_trkpt_disp_cb(const Waypoint* wpt) -{ +static void xol_trkpt_disp_cb(const Waypoint* wpt) { double x, y; - if (! GPS_Math_WGS84_To_Swiss_EN(wpt->latitude, wpt->longitude, &x, &y)) { + if (!GPS_Math_WGS84_To_Swiss_EN(wpt->latitude, wpt->longitude, &x, &y)) { xol_fatal_outside(wpt); } - gbfprintf(fout, "%*swriteStartElement("shape"); + writer->writeAttribute("type", "waypoint"); if (wpt->creation_time.isValid()) { - xol_write_time(wpt); + writer->writeAttribute("timestamp", wpt->CreationTimeXML()); } if (wpt->altitude != unknown_alt) { - gbfprintf(fout, " alt=\"%.f\"", wpt->altitude); + writer->writeAttribute("alt", QString::number(wpt->altitude, 'f')); } - gbfprintf(fout, ">\n"); - gbfprintf(fout, "%*s\n", space++*2, ""); - gbfprintf(fout, "%*s\n", space*2, "", x, y); - gbfprintf(fout, "%*s\n", --space*2, ""); - gbfprintf(fout, "%*s\n", --space*2, ""); + + writer->writeStartElement("points"); + writer->writeStartElement("point"); + writer->writeAttribute("x", QString::number(x)); + writer->writeAttribute("y", QString::number(y)); + writer->writeEndElement(); // point + writer->writeEndElement(); // points + writer->writeEndElement(); // shape } -static void -xol_write(void) -{ +static void xol_write(void) { double x, y; waypt_disp_all(xol_waypt_bound_calc); track_disp_all(NULL, NULL, xol_waypt_bound_calc); - if (! waypt_bounds_valid(&all_bounds)) { + if (!waypt_bounds_valid(&all_bounds)) { fatal(MYNAME ": No data available!\n"); } - if (! GPS_Math_WGS84_To_Swiss_EN( - (all_bounds.min_lat + all_bounds.max_lat) / 2, - (all_bounds.min_lon + all_bounds.max_lon) / 2, &x, &y)) { - gbfprintf(fout, "#####\n"); - fatal(MYNAME ": At least one point is outside of convertable area \"%s\"!\n", + if (!GPS_Math_WGS84_To_Swiss_EN((all_bounds.min_lat + all_bounds.max_lat) / 2, + (all_bounds.min_lon + all_bounds.max_lon) / 2, + &x, &y)) { + fatal(MYNAME + ": At least one point is outside of convertable area \"%s\"!\n", gt_get_mps_grid_longname(grid_swiss, MYNAME)); } - gbfprintf(fout, "\n", CSTR(global_opts.charset_name)); - gbfprintf(fout, "\n"); - gbfprintf(fout, "%*s\n", space++*2, ""); - gbfprintf(fout, "%*s
\n", space*2, "", x, y); - gbfprintf(fout, "%*s\n", space++*2, ""); + writer->setAutoFormatting(true); + writer->writeStartElement("overlays"); + writer->writeStartElement("overlay"); + writer->writeAttribute("version", "1.0"); + writer->writeStartElement("center"); + writer->writeAttribute("x", QString::number(x)); + writer->writeAttribute("y", QString::number(y)); + writer->writeEndElement(); // center + writer->writeStartElement("shapes"); + waypt_disp_all(xol_waypt_disp_cb); - track_disp_all(xol_track_hdr_disp_cb, xol_track_tlr_disp_cb, xol_trkpt_disp_cb); - gbfprintf(fout, "%*s\n", --space*2, ""); - gbfprintf(fout, "%*s\n", --space*2, ""); - gbfprintf(fout, "\n"); + track_disp_all(xol_track_hdr_disp_cb, xol_track_tlr_disp_cb, + xol_trkpt_disp_cb); + + writer->writeEndElement(); // shapes + writer->writeEndElement(); // overlay } -ff_vecs_t xol_vecs = { - ff_type_file, - { - (ff_cap)(ff_cap_read | ff_cap_write), /* waypoints */ - (ff_cap)(ff_cap_read | ff_cap_write), /* tracks */ - ff_cap_none - }, /* routes */ - xol_rd_init, - xol_wr_init, - xol_rd_deinit, - xol_wr_deinit, - xol_read, - xol_write, - NULL, - xol_args, - CET_CHARSET_UTF8, 0 -}; +ff_vecs_t xol_vecs = {ff_type_file, + {(ff_cap)(ff_cap_read | ff_cap_write), /* waypoints */ + (ff_cap)(ff_cap_read | ff_cap_write), /* tracks */ + ff_cap_none}, /* routes */ + xol_rd_init, + xol_wr_init, + xol_rd_deinit, + xol_wr_deinit, + xol_read, + xol_write, + NULL, + xol_args, + CET_CHARSET_UTF8, + 0}; -- 2.30.2